<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    width="50%"
    :maskClosable="false"
    :onClose="handleClose"
  >
    <BasicTable @register="registerTable">
      <template #form-advanceBefore>
        <a-button color="success" @click="handleCreate" style="margin-right: 10px"> 新增 </a-button>
      </template>
      <!-- <template #toolbar> </template> -->
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除？',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
  </BasicDrawer>
  <DictionaryConfigModal @register="registerConfigModal" @success="handleSuccess" />
</template>
<script lang="ts" setup>
  import { computed } from 'vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
  import { useModal } from '@/components/Modal';
  import DictionaryConfigModal from './DictionaryConfigModal.vue';

  import { columnsConfig, searchConfigFormSchema } from './dictionary.data';
  import { getDictionaryList, deleteById } from '@/api/system/dictionary';

  import { useMessage } from '@/hooks/web/useMessage';
  import { useI18n } from '@/hooks/web/useI18n';

  const { createMessage } = useMessage();
  const { t } = useI18n();

  let dicId: Number;
  const [registerConfigModal, { openModal }] = useModal();
  const [registerModal, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    getForm().resetFields();
    setDrawerProps({ confirmLoading: false });
    dicId = data.record?.id;
    reload();
  });

  const getTitle = computed(() => '字典管理');

  const [registerTable, { reload, getForm }] = useTable({
    // title: '开启搜索区域',
    api: getDictionaryList,
    columns: columnsConfig,
    useSearchForm: true,
    formConfig: {
      labelWidth: 80,
      schemas: searchConfigFormSchema,
    },
    showTableSetting: false,
    tableSetting: { fullScreen: true },
    showIndexColumn: false,
    rowKey: 'id',
    size: 'small',
    bordered: true,
    showSelectionBar: false, // 显示多选状态栏
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
    },
    beforeFetch: (param) => {
      param['parent_id'] = dicId;
    },
  });

  function handleCreate(record: Recordable) {
    openModal(true, {
      record,
      dictionaryId: dicId,
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    const { code, message } = await deleteById(record['id']);
    if (code != 0) {
      createMessage.error(message);
    } else {
      createMessage.success(t('sys.api.deleteSuccess'));
      reload();
    }
  }

  function handleClose() {
    closeDrawer();
  }

  function handleSuccess() {
    reload();
  }
</script>
<style lang="css">
  .vben-basic-drawer .ant-drawer-body {
    background-color: #f5f5f5;
  }

  .vben-basic-drawer .ant-drawer-body .scrollbar__wrap {
    padding: 0 !important;
  }
</style>
